from django.db import models
from yushiapi.path import * 
# Create your models here.

class Role(models.Model):
    _id = models.BigAutoField(primary_key=True)
    name = models.CharField(max_length=10,null=False)
    class Meta:
        db_table = "role"

class Grade(models.Model):
    _id = models.BigAutoField(primary_key=True)
    name = models.CharField(max_length=10,null=False)
    class Meta:
        db_table = "grade"

class Edition(models.Model):
    _id = models.BigAutoField(primary_key=True)
    name = models.CharField(max_length=10,null=False)
    class Meta:
        db_table = "edition"

class YuShiUser(models.Model):
    _id = models.BigAutoField(primary_key=True)
    union_id = models.CharField(max_length=28,null=True,unique=True)
    yushi_id = models.CharField(max_length=14,null=False)
    nick_name = models.CharField(max_length=50,default="anonymous")
    avator = models.ImageField('avator',upload_to=userAvatorPath,null=True,max_length=150)
    regist_time = models.DateTimeField( auto_now_add=True)
    role = models.ForeignKey(to=Role, on_delete=models.DO_NOTHING,null=True)
    balance = models.FloatField(default=0.0)

    class Meta:
        db_table = "yushi_user"

class Organization(models.Model):
    yushi_user = models.OneToOneField(to=YuShiUser, on_delete=models.CASCADE,primary_key=True)
    name = models.CharField(max_length=100,null=False)
    iphone = models.CharField(max_length=11,null=False)
    email = models.CharField(max_length=23,null=False)
    introduce = models.CharField(max_length=300,null=False)
    logo = models.ImageField('logo',upload_to=origanizationLogoPath,null=True,max_length=150)
    score = models.FloatField(default=4.5)

    class Meta:
        db_table = "organization"

class StudentWants(models.Model):
    _id = models.BigAutoField(primary_key=True)
    organization = models.ForeignKey(to=Organization, on_delete=models.CASCADE)
    content = models.TextField(default="There's nothing left!")

    class Meta:
        db_table = "student_wants"

class Teacher(models.Model):
    yushi_user = models.OneToOneField(to=YuShiUser, on_delete=models.CASCADE,primary_key=True)
    name = models.CharField(max_length=100,null=False)
    iphone = models.CharField(max_length=11,null=False)
    email = models.CharField(max_length=23,null=False)
    introduce = models.CharField(max_length=300,null=False)
    avator = models.ImageField('logo',upload_to=teacherAvatorPath,null=True,max_length=150)
    score = models.FloatField(default=4.5)
    organization = models.ForeignKey(to=Organization,null=True,on_delete=models.DO_NOTHING)

    class Meta:
        db_table = "teacher"

class Comment(models.Model):
    _id = models.BigAutoField(primary_key=True)
    content = models.TextField(default="There's nothing left!")
    score = models.FloatField(default=4.5)
    yushi_user = models.ForeignKey(to=YuShiUser, on_delete=models.CASCADE)
    teacher = models.ForeignKey(to=Teacher, on_delete=models.CASCADE,null=True)
    organization = models.ForeignKey(to=Organization, on_delete=models.CASCADE,null=True)
    create_time = models.DateTimeField( auto_now_add=True)

    class Meta:
        db_table = "comment"

class Classroom(models.Model):
    _id = models.BigAutoField(primary_key=True)
    creater = models.ForeignKey(to=YuShiUser, on_delete=models.CASCADE)
    organization = models.ForeignKey(to=Organization,on_delete=models.DO_NOTHING,null=True)
    name = models.CharField(max_length=20,default="NO NAME")
    identifier = models.CharField(max_length=10,null=False,unique=True)
    slogan = models.CharField(max_length=250,default="Fighting")
    create_time = models.DateTimeField(auto_now_add=True)
    image = models.ImageField(upload_to=classroomImagePath,null=False,max_length=200)

    class Meta:
        db_table = "classroom"

class UserFile(models.Model):
    _id = models.BigAutoField(primary_key=True)
    uploader = models.ForeignKey(to=YuShiUser, on_delete=models.CASCADE)
    name = models.CharField(max_length=100,default="NOTHIN")
    price = models.FloatField(default=0.0)
    _file = models.FileField(upload_to=userFilePath,max_length=256,null=False)
    create_time = models.DateTimeField(auto_now_add=True)
    classroom = models.ForeignKey(to=Classroom,on_delete=models.SET_NULL,null=True)
    share = models.BooleanField(default=False)

    class Meta:
        db_table = "userfile"

class Student(models.Model):
    _id = models.BigAutoField(primary_key=True)
    yushi_user = models.ForeignKey(to=YuShiUser,on_delete=models.CASCADE)
    classroom = models.ForeignKey(to=Classroom,on_delete=models.CASCADE)
    nick_name = models.CharField(max_length=30,default="YUSHI")

    class Meta:
        db_table = "student"
        unique_together = ("classroom","yushi_user")